package com.game.battleship.manager;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
 * This class implements the logic for manipulation with the forbiddenFieldsPositions.
 * 
 * 
 * @author vasil.konstantinov
 * @editor vtkhanh
 *
 */
public class ForbiddenPositionsManager implements Serializable {	
	
	private static final long serialVersionUID = 7554218396166492471L;
	
	/*It is important to store the count of times a field is forbidden
	 * so that this class can be reused in the ship positioning implementation*/		
	private Map<Short, Short> mForbiddenCellsHolder;
	
	/**
	 * 
	 */
	public ForbiddenPositionsManager() {
		this.mForbiddenCellsHolder = new HashMap<Short, Short>();
	}

	/**
	 * Stores a forbidden key in a data structure.In this implementation map is used 
	 * because it is important to remember how many times a field has been forbidden
	 * @param fc - the number of the forbidden field
	 */
	public void addForbiddenCell(short fc){
			if(mForbiddenCellsHolder.containsKey(fc)){
				short currentValue = mForbiddenCellsHolder.get(fc);
				mForbiddenCellsHolder.put(fc, (short) (currentValue + 1));
			}else{
				mForbiddenCellsHolder.put(fc, (short)1);
			}
	}
	
	/**
	 * Removes a forbidden field ONE time from the data structure.This means , that if cell was 
	 * forbidden more than once , it will still not be totaly removed , only the number of forbids 
	 * will be reduced with one.
	 * @param fc - the number of the forbidden field
	 */
	public void removeForbiddenCell(short fc){
		if(mForbiddenCellsHolder.containsKey(fc)){
			short currentValue = mForbiddenCellsHolder.get(fc);
			if(currentValue > 1){
				mForbiddenCellsHolder.put(fc, (short) (currentValue - 1));
			} else {
				mForbiddenCellsHolder.remove(fc);
			}
		}
	}
	/**	 
	 * @return - all forbidden fields added to the data structure
	 */
	public Set<Short> getAllForbiddenCells(){
		return mForbiddenCellsHolder.keySet();
	}
	
	/**
	 * 
	 * @param field
	 * @return whether or not a field is forbidden
	 */
	public boolean isForbidden(short field) {
		return mForbiddenCellsHolder.containsKey(field);
	}
}
